import Animated from 'react-native-reanimated'
import { WIDTH } from 'src/utils/ScreenUtil'

export const getInputRange = (pages) => {
  const inputRange = []
  for (let i = 0; i < pages; i++) {
    inputRange.push(i)
    if (i + 1 < pages) {
      inputRange.push(i + 0.5)
    }
  }
  return inputRange
}

export const getTranslate = (position, pages, viewWidth = WIDTH, indicationWidth) => {
  const itemWidth = viewWidth / 3
  const startPosition = (itemWidth - indicationWidth) / 2
  const inputRange = getInputRange(pages)
  return Animated.interpolate(position, {
    inputRange,
    outputRange: inputRange.map((value, index) => {
      let position
      const valueInt = parseInt(value)
      if (valueInt === value) { // 整数
        position = itemWidth * valueInt + startPosition
      } else {
        position = itemWidth * (value + 0.5) - (indicationWidth / 2)
      }
      return position
    })
  })
}

export const getScale = (position, pages, scaleVal = 1.5) => {
  const inputRange = getInputRange(pages)
  return Animated.interpolate(position, {
    inputRange,
    outputRange: inputRange.map((value) => {
      return parseInt(value) === value ? 1 : scaleVal
    })
  })
}
